<template>
  <div ref="FundsSpendingEdit" class="_wz_FundsSpendingEdit">
    <Card :bordered="true">
      <p slot="title">列表</p>
      <p slot="extra">
        <Button type="primary" @click="addTableData" v-ButtonPerm="'safety:expenditure:save'">添加</Button>
      </p>
      <Table stripe highlight-row ref="table" :loading="tableLoading" border :columns="columns2" :data="tableData" :height="tableHeight"></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page ref="tablePage" :total="tableTotal" show-sizer :current="page" show-elevator show-total @on-change="changePage" @on-page-size-change="changePageSize" />
        </div>
      </div>
    </Card>


    <Drawer :title="formTitle" placement="left" v-model="formOperation" width="700" :mask-closable="false" :draggable="true" :styles="formOperationStyles">
      <securityFundsAdd v-if="formOperation" @closeFun="closeFun" :selectdata="selectdata" :summaryData="summaryData"></securityFundsAdd>
    </Drawer>
  </div>
</template>
<script>


  import securityFundsAdd from "./FundsSpendingAdd";

  export default {
    components: {
      securityFundsAdd,//营运证详情
    },
    props: ["summaryData"],//接收来自父组件的数据
    data() {
      return {
        formTitle: '',
        selectdata: null,
        formOperation: false,//表单左侧移出
        formOperationStyles: {//表单左侧移出css
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          padding: '10px 10px 53px 10px',
          position: 'static'
        },
        tableLoading: true,//表格加载
        columns2: [//表格表头数据
          {type: 'index', width: 70, align: 'center', title: '序号'},
          {title: '运输企业', minWidth: 200, key: 'deptName'},
          {title: '安全费用名目', minWidth: 180, key: 'planItemName'},
          {
            title: '投入使用月份', minWidth: 120, key: 'expenditureMonth', render: (h, params) => {
              return h('span', params.row.expenditureMonth + '月份')
            }
          },
          {title: '提取金额', minWidth: 120, key: 'expenditureExtractAmount'},
          {title: '支出金额', minWidth: 120, key: 'expenditureAmount'},
          {title: '备注', minWidth: 160, key: 'remark'},
          {
            title: '操作', fixed: 'right', width: 140,
            render: (h, params) => {
              var operationCertificate = [];
              //修改
              if (JSON.parse(sessionStorage.getItem('loginData')).authority.SAFETY.includes("safety:expenditure:update")) {
                operationCertificate = [
                  h('Button', {
                    props: {size: "small", type: "warning"},
                    style: {"margin": "0px 5px 0 0"},
                    on: {
                      click: () => {
                        if (params.row.refVehicleState == 1 || params.row.refVehicleState == 2 || params.row.refVehicleState == 3) {
                          this.$Message.error('这辆车不存在，不可修改');
                        } else {
                          this.modifyTableData(params.row)
                        }
                      }
                    }
                  }, "修改")
                ];
              }

              //删除
              if (JSON.parse(sessionStorage.getItem('loginData')).authority.SAFETY.includes("safety:expenditure:delete")) {
                operationCertificate.push(
                  h('Button', {
                    props: {size: "small", type: "error"},
                    style: {"margin": "0px 5px 0 0"},
                    on: {
                      click: () => {
                        this.removeTableFun(params.row)
                      }
                    }
                  }, "删除")
                );
              }
              return h('div', operationCertificate);
            }
          },
        ],
        page: 1,//当前页码
        limit: 10,//当前页码
        tableTotal: 0,//总条数
        tableData: [],//表格数据
        tableHeight: 450,//表格默认高度
      }
    },
    beforeCreate() {//beforeCreate创建前状态

    },
    created() {//created创建完毕状态
    },
    beforeMount() {//beforeMount挂载前状态   DOM 元素然后塞入页面的过程

    },
    mounted() {//mounted 挂载结束状态
      var that = this;
      that.tableLoadData();
      that.$nextTick(function () {
        var tabHeight = 0;
        if (!!localStorage.getItem('systemStyle')) {
          var systemStyle = JSON.parse(localStorage.getItem('systemStyle'));
          tabHeight = !!systemStyle.multiTabbed ? 32 : 0;
        }

        that.tableHeight = window.innerHeight - 51 - 32 - 51 - 32 - 34 - 20 - tabHeight;
        window.onresize = function () {
          that.tableHeight = window.innerHeight - 51 - 32 - 51 - 32 - 34 - 20 - tabHeight;
        };
      })

    },
    methods: {//执行的方法
      tableLoadData() {//加载表格数据
        var that = this;
        that.tableLoading = true;//加载
        var searchCriteria = {
          page: that.page,
          imit: that.limit,
          planId: that.summaryData.id
        }
        that.axios.get(that.apiUrl.safetyBudgetExpenditurePageList, {params: searchCriteria})
        .then(res => {
          if (!!res) {
            that.tableLoading = false;
            that.tableTotal = res.data.count;
            that.tableData = res.data.data;//返回数据
          }
        }).catch(err => {
          console.log("失败", err)
        })
      },
      changePage(page) {//返回第几页
        this.page = page;//设置当前页数
        this.tableLoadData();
      },
      changePageSize(size) {
        this.limit = size;//设置当前页数
        this.tableLoadData();
      },
      addTableData() {//table添加
        var that = this;
        that.formTitle = "安全经费添加";
        that.selectdata = null;
        that.formOperation = true;
      },
      modifyTableData(data) {//修改
        var that = this;
        that.formTitle = "安全经费修改";
        that.selectdata = data;
        that.formOperation = true;
      },
      removeTableFun(data) {//删除
        var that = this;
        that.$Modal.confirm({
          title: '删除提醒',
          content: '确定要删除选中的记录？',
          onOk: () => {
            that.$Spin.show();
            that.axios.post(that.apiUrl.safetyBudgetExpenditureDelete + data.id)
            .then(res => {
              that.$Spin.hide();
              if (!!res) {
                that.$Message.success('数据删除成功.');
                that.tableLoadData();
              }
            }).catch(err => {
              console.log("失败", err)
            })
          },
          onCancel: () => {
            //点击取消
          }
        });
      },
      searchSubmit() {//搜索
        this.page = 1;//设置当前页数
        this.searchCriteria = JSON.parse(JSON.stringify(this.formDataSearch));
        this.tableLoadData();
      },
      //添加修改触发后关闭刷新
      closeFun(data) {
        var that = this;
        if (data) {//添加修改 刷新表单
          that.formOperation = false;
          that.tableLoadData();
        } else {//取消关闭  不刷新
          that.formOperation = false;
        }
      },
    },
    watch: {//监听

    },
    beforeUpdate() {//beforeUpdate 更新前状态

    },
    updated() {//updated 更新完成状态

    },
    beforeDestroy() {//beforeDestroy 销毁前状态

    },
    destroyed() {//destroyed 销毁完成状态

    }
  }
</script>
<style lang="less">
  ._wz_FundsSpendingEdit {
    height: 100%;
  }
</style>
